// pages/EditProblem/index.js
import {
  HTTP
} from '../../../utils/http.js'
let Myhttp = new HTTP()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    questionnaire: [{
      radioshow: true
    }],
    qid: '',
    checked: [],
    delete: false,
    price:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //  console.log('2')
    // console.log(options)
    let that = this
    if (options.qid) {
      that.setData({
        qid: options.qid
      })
    } else {
      that.setData({
        qid: '123'
      })
    }
    //得到具体问卷信息
    // Myhttp.request({
    //   url: 'questionnaire/questionnaire/editQuestionnaire',
    //   data: {
    //     qid: this.data.qid
    //   },
    //   success: (res) => {
    //     console.log(res)
    //     let datas = res.data.data
    //     datas.src = datas.url_domin + datas.cover
    //     that.setData({
    //       'questionnaire[0]': datas,
    //        price: datas.price
    //     })
        
    //   }

    // })
    // this.setData({

    // })

  },
  publish2: function() {
    Myhttp.jump({
      url: `/pages/PublishQuestionnaire/PublishProblems/index?qid=${this.data.qid}`
    })
    // wx.navigateTo({
    //   url: `/pages/PublishQuestionnaire/PublishProblems/index?qid=${this.data.qid}`,
    // })
  },
  delete: function(e) {
    
    let list = this.data.questionnaire[0].problem_list
    
    for(let i=0;i<list.length;i++){
      list[i].checked = false
    }

     let lists = `questionnaire[${0}].problem_list`

    this.setData({
      delete: true,
      lists:list
    })
    // console.log(this.data.questionnaire[0])
  },
  delete2: function(e) {
    let list = this.data.questionnaire[0].problem_list
    let che = []
    list.forEach((val, index) => {
      if (val.checked) {
        che.push(val.id)
      }
    })
    this.setData({
      checked: che
    })

    let datas = this.data.checked

    let that = this
    Myhttp.request({
      url: 'questionnaire/questionnaire/delProblem',
      data: {
        qid: this.data.qid,
        pids: datas
      },
      success: (res) => {
        // console.log(res)
        Myhttp.request({
          url: 'questionnaire/questionnaire/editQuestionnaire',
          data: {
            qid: that.data.qid
          },
          success: (res) => {
            this.setData({
              checked: [],
              delete: false,
            })

            let datas = res.data.data
            that.setData({
              'questionnaire[0]': datas,
               price: datas.price
            })
            console.log(res)
          }
        })
      }

    })
  },
  radioChange: function(e) {
    
    let index = e.target.dataset.index
    //console.log(index)
    let list = this.data.questionnaire[0].problem_list
   // console.log(list)
  
    list[index].checked = !list[index].checked

    // console.log(list[index])
    // console.log(list)

    // let aa = this.data.checked
    // aa[index] = list[index].id

    // let checked = `checked[${index}]`
    let lists = `questionnaire[${0}].problem_list`
    this.setData({
      lists: list
    })
    //console.log(this.data.checked)
  },
  clickList:function(e){
    let id = e.currentTarget.dataset.id
    Myhttp.jump({
      url: `/pages/PublishQuestionnaire/PublishProblems/index?id=${id}&qid=${this.data.qid}`
    })
    // wx.navigateTo({
    //   url: `/pages/PublishQuestionnaire/PublishProblems/index?id=${id}&qid=${this.data.qid}`,
    // }) 
  },
  final: function() {
    
    let that = this
    if(this.data.price > 0){
      Myhttp.request({
        url: 'questionnaire/questionnaire/toPay',
        data: {
          qid: that.data.qid
        },
        success: (res) => {
          let data = res.data.data
          wx.requestPayment({
            timeStamp: data.timeStamp,
            package: data.package,
            nonceStr: data.nonceStr,
            signType: data.signType,
            paySign: data.paySign,
            'success': function (res) {
              Myhttp.jump({
                method:'switchTab',
                url: '/pages/personal/personal_details/index'
              })
              // wx.switchTab({
              //   url: '/pages/personal/personal_details/index',
              // })
            },
            'fail': function (res) {
              wx.showToast({
                title: '支付失败',
                mask:true
              })
            }
          })
         // console.log(res)
        }
      })
    }else{
      Myhttp.jump({
        method: 'switchTab',
        url: '/pages/personal/personal_details/index'
      })
      // wx.reLaunch({
      //   url: '/pages/personal/personal_details/index',
      // })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      checked: [],
      delete: false,
      price: '',
    })
    
    let that = this
    //得到具体问卷信息
    Myhttp.request({
      url: 'questionnaire/questionnaire/editQuestionnaire',
      data: {
        qid: this.data.qid
      },
      success: (res) => {
        let datas = res.data.data
        datas.src = datas.url_domin + datas.cover
        that.setData({
          'questionnaire[0]': datas,
          price: datas.price
        })
        console.log(res)
      }

    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    let that = this
    this.setData({
      checked: [],
      delete: false,
    })
    Myhttp.request({
      url: 'questionnaire/questionnaire/editQuestionnaire',
      data: {
        qid: this.data.qid
      },
      success: (res) => {
        console.log(res)
        let datas = res.data.data
        datas.src = datas.url_domin + datas.cover
        that.setData({
          'questionnaire[0]': datas,
          price: datas.price
        })
        wx.stopPullDownRefresh() //停止下拉刷新
      }

    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      path: `/pages/PublishQuestionnaire/EditProblem/index?qid=${this.data.qid}`
    }
  }
})